package com.addzero.web.ui.hooks.tree

// 移除对UseList的依赖，我们将用递归渲染树
// import com.addzero.web.ui.hooks.list.UseList
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Icon
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.addzero.web.ui.hooks.UseHook
import com.addzero.web.ui.hooks.table.table.UseSearch
import com.addzero.web.ui.hooks.tree.render_command.UiFunctionCommand


class UseListTree<T>(
    private val enableFunctionCommand: List<UiFunctionCommand> = listOf(
        UiFunctionCommand.SEARCH,
//        UiFunctionCommand.EDITMODE
    ),
    val initialNodes: List<T>,
    val getIdFun: (T) -> Any = { it.hashCode() },
    val getChildren: (T) -> List<T>,
    val getLaebel: (T) -> String,
    val getNodeTypeFun: (T) -> NodeTypes,
) : UseHook<UseListTree<T>> {

    private val useEditMode = UseEditMode(enableFunctionCommand.contains(UiFunctionCommand.EDITMODE))

    val useTreeNodeItem = UseTreeNodeItem(
        useEditMode.editFlag, getLaebel, getChildren = getChildren, getNodeTypeFun =
            getNodeTypeFun, getIdFun = getIdFun
    )

    // 搜索查询状态
    val useSearch = UseSearch()

    // 基本的树过滤函数，如果查询为空则返回所有节点
    private fun filterNodes(nodes: List<T>): List<T> {
        if (useSearch.searchText.isBlank()) return nodes

        // 递归过滤树的辅助函数
        fun filterNodeRecursive(node: T): Boolean {
            val nodeLabel = getLaebel(node)
            val nodeChildren = getChildren(node)

            // 如果节点标签包含搜索词，或者任何子节点包含搜索词，返回true
            return nodeLabel.contains(useSearch.searchText, ignoreCase = true) ||
                    nodeChildren.any { filterNodeRecursive(it) }
        }

        // 保留任何包含搜索词的节点，或者有子节点包含搜索词的节点
        return nodes.filter { filterNodeRecursive(it) }
    }

    // 递归渲染树的组件
    @Composable
    private fun RenderTreeNodes(nodes: List<T>, currentLevel: Int = 0) {
        nodes.forEach { node ->
            // 使用UseTreeNodeItem来渲染单个节点
            useTreeNodeItem.apply<UseTreeNodeItem<T>> {
                updateNode(node)
                level = currentLevel // 自动计算当前层级
                expanded = true // 默认展开所有节点
            }.render {}

            // 不在这里递归渲染子节点
            // UseTreeNodeItem内部会处理子节点的渲染
        }
    }

    override val render: @Composable () -> Unit
        get() = {
            Box(modifier = modifier.fillMaxWidth()) {
                Column {
                    if (enableFunctionCommand.contains(UiFunctionCommand.SEARCH)) {
                        useSearch.render {}
                        useEditMode.render {}
                    }

                    // 过滤节点并递归渲染
                    val filteredNodes = remember(initialNodes, useSearch.searchText) {
                        filterNodes(initialNodes)
                    }

                    // 输出调试信息
                    if (filteredNodes.isEmpty()) {
                        Text("没有匹配的节点", modifier = Modifier.padding(8.dp))
                    }

                    // 直接使用Column递归渲染而不是LazyColumn
                    Column(modifier = Modifier.fillMaxWidth()) {
                        RenderTreeNodes(filteredNodes)
                    }
                }
            }
        }
}

@Composable
fun <T> UseListTree<T>.AddSearch() {
    var searchText by mutableStateOf("")

    OutlinedTextField(
        value = searchText,
        onValueChange = { searchText = it },
        modifier = Modifier.fillMaxWidth().padding(8.dp),
        placeholder = { Text("搜索...") },
        leadingIcon = { Icon(Icons.Default.Search, contentDescription = "搜索") },
        singleLine = true
    )
}
